"use strict";
/* Controllers */
app.controller("alertRuleListController", ['$scope', '$location', '$rootScope', '$routeParams', 'alertRuleListService', function ($scope, $location, $rootScope, $routeParams, alertRuleListService) {

    var vm = $scope.vm = {};
    vm.p = {};
    vm.result = {};


    var errorFn = function (result) {
        alert(result);
    };

    $scope.listAlertPolicy = function () {
        var vm = $scope.vm;
        vm.result.ms = "...";
        var params = {};
        var procFn = function (response) {
            vm.result.ms = response.ms;
            var data = response.data;
            if (data == null) return;
            vm.result.data = data;
            $('#list_table').bootstrapTable('load', data);
        };
        alertRuleListService.listAlertPolicy(params, procFn, errorFn);
    };

    $scope.initializeController = function () {
        vm.result.ms = '';
        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
             pageList: [30, 50, 100, 300, 500, 1000], pageSize: 30
        });
//        $resultTable.bootstrapTable('hideColumn', 'action');
        $scope.listAlertPolicy();
    };

    $scope.initializeController();

}]);


/* Services */
app.service('alertRuleListService', ['ajaxService', function (ajaxService) {

    this.listAlertPolicy = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/alert_rule/listAlertPolicy.do", successFunction, errorFunction);
    };

    this.deleteAlertPolicy = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/alert_rule/deleteAlertPolicy.do", successFunction, errorFunction);
    };
}]);

function queueTypeFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    switch (value) {
        case 0:
            return "动态";
        case 1:
            return "固定";
        default:
            return "-";
    }
}

function alertRuleIdOptFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    return [
        '<a href="#/bcp/alert/alert_rule_add/id/', value,'" target="_blank">', "<u>修改</u>" , '</a>'
    ].join('');
}
function ruleCodeFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    return [
        '<a href="#/bcp/alert/alert_rule_add/code/', value,'" target="_blank">', value , '</a>'
    ].join('');
}


function dutyPolicyFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    try {
        var obj = JSON.parse(value);
        return [
            value,'<br/>',
            '<a href="/onduty/index.html#/star/onduty/resolvegroup_add/code/', obj.resolvegroupId,'" target="_blank">', "<u>支持组</u>" , '</a> ',
            '<a href="/onduty/index.html#/star/onduty/cti_add/', obj.ctiId,'" target="_blank">', "<u>支持项</u>" , '</a>'
        ].join('');
    } catch(e) {
        return [
            value, '<br/>',
            e
        ].join('');
    }
}



